@extends('layouts.default')

@section('title', '经验交流')

@push('scripts')
<script src="{{ asset('lib/xm-select-v1.2.4/xm-select.js')}}"></script>
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage','upload'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var upload = layui.upload;
        var xmDemo;
        var zmrDemo;
        var fzrDemo;
        var PageId = "{{request()->get('id',0)}}";

        renderSelect();

        laydate.render({
            elem: '#begin-date'
            ,type: 'date'
        });

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            await renderParticipants();
            await renderZmrSelector();
            await renderFzrSelector();
            pageInit();
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['LEVEL', 'SUBJECT', 'GRADE', 'reception_class']}, function (res){
                    if(res.code === 1) {
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];

                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }

                        form.render();

                        resolve();
                    }
                });
            });
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }

                        form.render('select');

                        resolve();
                    }
                });
            });
        }

        //参与教师下拉框数据渲染
        function renderParticipants() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        xmDemo = xmSelect.render({
                            el: '#pter',
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },
                            filterable: true,
                            height: 'auto',
                            name:'participant',
                            layVerify: 'required',
                            layVerType: 'msg',
                            layReqText:'参与教师不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            model: {
                                label: {
                                    type: 'block',
                                    block: {
                                        //最大显示数量, 0:不限制
                                        showCount: 0,
                                        //是否显示删除图标
                                        showIcon: false,
                                    }
                                }
                            },
                            disabled:true,
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        })

                        return resolve();
                    }
                });
            });
        }
        //证明人下拉框数据渲染
        function renderZmrSelector() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        zmrDemo = xmSelect.render({
                            el: '#zmr-xms',
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },
                            direction: 'down',
                            filterable: true,
                            height: 'auto',
                            name:'zmr',
                            // layVerify: 'required',
                            // layVerType: 'msg',
                            // layReqText:'证明人不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            model: {
                                label: {
                                    type: 'block',
                                    block: {
                                        //最大显示数量, 0:不限制
                                        showCount: 0,
                                        //是否显示删除图标
                                        showIcon: false,
                                    }
                                }
                            },
                            disabled:true,
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        });

                        return resolve();
                    }
                });
            });
        }
        //证明人部门负责人下拉框数据渲染
        function renderFzrSelector() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        fzrDemo = xmSelect.render({
                            el: '#fzr-xms',
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },
                            direction: 'down',
                            filterable: true,
                            height: 'auto',
                            name:'fzr',
                            // layVerify: 'required',
                            // layVerType: 'msg',
                            // layReqText:'证明人部门负责人不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            model: {
                                label: {
                                    type: 'block',
                                    block: {
                                        //最大显示数量, 0:不限制
                                        showCount: 0,
                                        //是否显示删除图标
                                        showIcon: false,
                                    }
                                }
                            },
                            disabled:true,
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        });

                        return resolve();
                    }
                });
            });
        }

        // 文件显示
        function fileShow(files) {
            let showFiles = {};
            layui.each(files, function(i, d) {
                if(d.field) {
                    if (showFiles[d.field]) {
                        showFiles[d.field].push(d);
                    } else {
                        showFiles[d.field] = [d];
                    }
                } else {
                    if (showFiles['zmd']) {
                        showFiles['zmd'].push(d);
                    } else {
                        showFiles['zmd'] = [d];
                    }
                }
            });

            layui.each(showFiles, function(f, d) {
                let fileShowTpl = fileShowDemo.innerHTML
                    ,fileShow = document.getElementById('file-show-'+f);

                laytpl(fileShowTpl).render(d, function(html){
                    fileShow.innerHTML = html;
                });
            });
        }

        //数据渲染
        function pageInit(){
            getActivityOne({id:PageId}, function(res) {
                if(res.code === 1) {
                    let Activity = res.data;

                    levelChange(Activity.level_array[0].optionId);
                    if(Activity.zm_style == 1) {
                        $(".gkk-category").removeClass('layui-hide');
                    }

                    form.val('infoForm', {
                        'id': PageId,
                        'data_version_id': Activity.data_version_id,
                        'name': Activity.name,
                        'begin_date': Activity.begin_date,
                        'level': Activity.level_array[0].optionId,
                        'course': Activity.course_set[0].optionId,
                        'grade': Activity.grade_array[0].optionId,
                        'reception_class': Activity.reception_class && Activity.reception_class_set[0].optionId,
                        'activity_category': Activity.activity_category,
                        'created_at': Activity.created_at,

                        // 'zm_style': Activity.zm_style,
                        // 'zm_gkk_category': Activity.zm_gkk_category,
                        'witness_position': Activity.witness_position,
                        'place': Activity.place,
                        'target': Activity.target,
                        'zmd_remark': Activity.zmd_remark
                    });
                    form.val('checkForm', {
                        'form_id': Activity.id,
                        'form_instance_id': Activity.form_instance_id,
                        'status': Activity.instance.status,
                        'comment': Activity.instance.comment,
                        'activity_category': Activity.activity_category
                    });

                    // 创建人
                    if(Activity.creator){
                        var creatorName = '<button class="layui-btn layui-btn-primary" type="button">' + Activity.creator.nickname + '</button>';

                        $("#creator-show").append(creatorName);
                    }

                    if(Activity.level_array[0].optionId >= 111) {
                        if(Activity.witness_set) {
                            zmrDemo.setValue(Activity.witness_set.zmrs);
                        }
                        if(Activity.headdepart_set) {
                            fzrDemo.setValue(Activity.headdepart_set.fzrs);
                        }
                    }

                    //参与教师
                    xmDemo.setValue(Activity.participants_users.pant);
                    //文件
                    if(Activity.proof_material_array.length){
                        fileShow(Activity.proof_material_array);
                    }
                } else {
                    layer.msg('无数据记录', {time: 2000, icon: 5});
                }
            });
        }

        form.on('select(levelList)', function(data){
            zmdFiles = [];
            $("#zmd-file").val('');
            $('#zmd-generate').parent().find('.file-show').html('');

            levelChange(data.value);
        });
        function levelChange(v) {
            if(v >= 111 && v != 850) {
                $(".zmd-item").removeClass('layui-hide');

                $("#zmd-uploader").addClass('layui-hide');
                $("#zmd-generate").removeClass('layui-hide');
            } else {
                $(".zmd-item").addClass('layui-hide');

                $("#zmd-uploader").removeClass('layui-hide');
                $("#zmd-generate").addClass('layui-hide');
            }
        }
        form.on('select(zmStyleList)', function(data){
            if(data.value == 1) {
                $(".gkk-category").removeClass('layui-hide');
            } else {
                $(".gkk-category").addClass('layui-hide');
            }
        });


        $(document).on('click','#zmd-generate',function(){
            form.submit('editForm', function(data){
                var field = data.field; // 获取表单全部字段值
                console.log(data); // 回调函数返回的 data 参数和提交事件中返回的一致

                field.zmr_ids = zmrDemo.getValue('value');
                field.fzr_ids = fzrDemo.getValue('value');

                if(field.zmr_ids.length <= 0){
                    layer.msg('证明人不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(field.fzr_ids.length <= 0){
                    layer.msg('证明人部门负责人不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(!field.witness_position){
                    layer.msg('证明人职务不能为空',{icon: 5,time: 1000});
                    return false;
                }
                // if(!field.zm_style){
                //     layer.msg('形式不能为空',{icon: 5,time: 1000});
                //     return false;
                // }
                // if(field.zm_style == 1 && !field.zm_gkk_category){
                //     layer.msg('公开课类别不能为空',{icon: 5,time: 1000});
                //     return false;
                // }
                if(!field.place){
                    layer.msg('地点不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(!field.target){
                    layer.msg('参与对象不能为空',{icon: 5,time: 1000});
                    return false;
                }

                generateCertificate(field, function(res){
                    if(res.code == '1'){
                        layer.msg(res.msg,{icon: 6,time: 1000},function(){
                            zmdFiles = res.data;
                            let files = zmdFiles;
                            $('#zmd-file').val(JSON.stringify(zmdFiles));

                            $('#zmd-generate').parent().find('.file-show').html('');
                            for(i in files) {
                                if(files[i]) {
                                    let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                                    $('#zmd-generate').parent().find('.file-show').append(fsHtml);
                                }
                            }
                        });
                    }else{
                        layer.msg(res.msg, {icon: 5,shift : 6});
                    }
                });
            });
        });

        //提交
        form.on('submit(sub)', function(data){
            let d = data.field;

            checkActivity(d, function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });

            return false;
        });
    });
</script>
@endpush

@section('content')
<style>
    .layui-row{margin-bottom: 10px;}
    .opt-panel{padding: 10px;}
    .panel-title h3{margin-bottom: 20px;font-weight: 800;}

    .opt-form{padding: 20px 5px;}
    .opt-area{display: flex;flex-direction: row-reverse;padding: 10px 20px;margin-bottom: 20px;}
    .opt-area button, .back-btn{margin-left: 10px;}
</style>

<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="info-form" lay-filter="infoForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>经验交流 - 查看</h3>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">学年学期：</label>
                        <div class="layuik-input-inline">
                            <select disabled class="data-version-id" name="data_version_id" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">主题名称：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="name" required lay-verify="required" placeholder="请输入" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">日期：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="begin_date" id="begin-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">级别：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="level" lay-filter="levelList" data-role="levelList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">学科：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="course" lay-filter="subjectList" data-role="subjectList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">研究课：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="reception_class" lay-filter="reception_classList" data-role="reception_classList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">类别：</label>
                        <div class="layuik-input-inline">
                            <input type="radio" name="activity_category" value="EW_EXCHANGE_EXPERIENCE_TABLE" title="教育" class="layui-radio">
                            <input type="radio" name="activity_category" value="TEARRESEARCH_EXCHANGE_TABLE" title="教学" class="layui-radio">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">年级：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="grade" lay-filter="gradeList" data-role="gradeList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>

                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline gkk-category layui-hide">
                        <label class="layuik-form-label"><i class="redstar">*</i>公开课类别：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="zm_gkk_category" lay-filter="zmgkkCategoryList" data-role="zmgkkCategoryList">
                                <option value="">请选择</option>
                                <option value="1">讲座</option>
                                <option value="2">交流</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>地点：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="place" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>参与对象：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="target" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">备注：</label>
                        <div class="layuik-input-inline">
                            <textarea disabled name="zmd_remark" id="" rows="3" class="layui-textarea" placeholder="请输入"></textarea>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">参与教师：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="pter" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>证明人：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="zmr-xms" class="xm-select-demo"></div>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>部门负责人：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="fzr-xms" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">证明人职务：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="witness_position" id="zmrzw" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">证明材料：</label>
                        <div class="layuik-input-inline">
                            <div id="file-show-zmd"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">创建人：</label>
                        <div class="layuik-input-inline" id="creator-show"></div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">创建时间：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="created_at" id="created_at" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="check-form" lay-filter="checkForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>经验交流 - 审核</h3>
                </div>

                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>审核结果：</label>
                        <div class="layuik-input-inline">
                            <input  type="radio" name="status" value="PASS" title="通过">
                            <input  type="radio" name="status" value="REVIEW_FAIL" title="驳回" >
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">原因：</label>
                        <div class="layuik-input-inline">
                            <textarea  name="comment" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="opt-area">
                    <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary back-btn">返回</a>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="sub">提交</button>
                    <input type="hidden" name="form_instance_id" value="0">
                    <input type="hidden" name="activity_category" value="">
                    <input type="hidden" name="form_id" value="0">
                </div>
            </div>
        </form>
    </div>
</div>

<script id="fileShowDemo" type="text/html">
<ul>
@{{#  layui.each(d, function(index, item){ }}
@{{#  if(item){ }}
    <li style="min-width: 200px;margin: 13px 5px;">
        <a style="color: #0e90d2;" target="_blank" title="@{{item.file_info.file_name}}" href="@{{item.file_info.file_dir}}@{{item.file_info.file_path}}">
            <span style="display: inline-block;max-width: 90%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">@{{item.file_info.file_name}}</span>
        </a>
        <a style="color:#74B8FF" href="@{{item.file_info.file_dir}}@{{item.file_info.file_path}}" download="@{{item.file_info.file_name}}">
            <span class="icon pear-icon pear-icon-download" style="color:#74B8FF;margin-left:10px;"></span>
        </a>
    </li>
@{{#  } }}
@{{#  }); }}
</ul>
</script>
@endsection
